<?php
include '../../../wp-blog-header.php';

if(!empty($_POST['user_id']))
{
  $api = new PodAPI();
  
  $columns_brand['name'] = pods_sanitize($_POST['brand_name']);
  $columns_brand['brandurl'] = sanitize_title($columns_brand['name']); 
  
  $brand = new Pod('brand');
  $where = 'brandurl = "'.$columns_brand['brandurl'].'"';
  $brand->findRecords('id DESC', '',$where);
  $brandFound = $brand->getTotalRows();
  
  if($brandFound != 1)
  {
    $columns_brand['location'] = pods_sanitize($_POST['store_add']);
    $columns_brand['local'] = '1';
    $params_brand = array('datatype' => 'brand', 'columns' => $columns_brand);
    $brand_id = $api->save_pod_item($params_brand);
  }
  else
  {
    while($brand->fetchRecord())
    {
      $brand_id = $brand->get_field('id');
    }
  }
  
  $columns_deal['name'] = pods_sanitize(ucwords(strtolower($_POST['deal_name'])));
  $columns_deal['camp_link'] = sanitize_title($columns_deal['name']);
  $columns_deal['city'] = $_POST['city_id'];
  $columns_deal['short'] = pods_sanitize($_POST['deal_desc']);
  $columns_deal['terms'] = pods_sanitize($_POST['tnc']);
  $columns_deal['camp_cat'] = $_POST['category_id'];
  $columns_deal['camp_start'] = date("Y-m-d H:i:s");
  if(empty($_POST['expires']))
  {
    $columns_deal['camp_end'] = date("Y-m-d H:i:s", strtotime("+3 months"));
  }
  else
  {
    $columns_deal['camp_end'] = date("Y-m-d H:i:s", strtotime($_POST['expires']));
  }
  $columns_deal['brand'] = $brand_id;
  $columns_deal['sale'] = '1';
  $user_id = $_POST['user_id'];
  $columns_deal['added_by_user'] = $user_id;
  
  $params_deal = array('datatype' => 'deal', 'columns' => $columns_deal);
  $deal_id = $api->save_pod_item($params_deal);
  
  $points = new Pod('points');
  $points->findRecords('id DESC', "1", "user.id = '$user_id'");
  while($points->fetchRecord())
  {
    $points_id = $points->get_field('id');
    $deals_points = $points->get_field('deals');
    $user_name = $points->get_field('user.name');
    $user_slug = $points->get_field('user.slug');
  }
  $columns_points['deals'] = ($deals_points + 500);
  $params_points = array('datatype' => 'points', 'tbl_row_id' => $points_id, 'columns' => $columns_points);
  $api->save_pod_item($params_points);
  
  echo '<img src="'.get_bloginfo('wpurl').'/wp-content/pfiles/images/success.gif" />&nbsp;Thank You! Your In-Store Deal has been Added, You Gained <b>500</b> Points!';
  
  $deal_name = $columns_deal['name'];
  $deal_link = $columns_deal['camp_link'];
  $brand_name = pods_sanitize($columns_brand['name']);
  $brand_url = $columns_brand['brandurl'];
  
  $msg=
      '<div id="body" style="width: 500px; border: solid; border-color: #FFFFFF; background-color: #FFFFFF;">
      <div id="logo" style="text-align: left">
      	<img style="margin-left: 178px; margin-top: 10px;" height="40" src="http://buzzr.in/wordpress/wp-content/uploads/2011/01/buzzrin-logo.png" />
      </div>
      <div id="content" style="width: auto; margin: 20px; padding: 5px; background-color: #D0F0FB; font-size: 14px; color:#1BA3DF;">
      Hi,<br /><br />
      <i><b>Chetan</b></i><br /><br />
      A New Sale has been added by <a href="'.get_bloginfo('url').'/member/'.$user_slug.'">'.$user_name.'</a><br /><br />
      Details of the Sale:<br /><b>Viewed at: </b><a href="'.get_bloginfo('url').'/deals/'.$deal_link.'">'.$deal_name.'</a><br />
      <a href="'.get_bloginfo('url').'/edit/'.$deal_link.'"><b>Edit it</b></a><br /><br />
      Details of the Brand:<br />
      <b>View Profile: </b><a href="'.get_bloginfo('url').'/brands/'.$brand_url.'">'.$brand_name.'<br />
      Thank You!
      </div>
      
      </div>';
      send_html_mail('Chetan Bhatia<chetan.bhatia@enthrallmedia.com>', 'New Sale has been Added!' , $msg, 'Buzzr.in<info@enthrallmedia.com>');
}
else
{
  echo '<h3>Please Login to Add a Deal!</h3>';  
}
?>